package com.hsck.order.kds.base.common

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Parcelable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.gson.Gson
import com.hjq.toast.Toaster
import java.text.SimpleDateFormat
import java.util.*

/**
 * 创建者 : ly
 * 时间 : 2017/9/13
 * 公司 : 工博
 * 功能 : 上下文的延展
 */
val Context.prefer: SharedPreferences
    get() = lazy { getSharedPreferences("application", Context.MODE_PRIVATE) }.value

var Context.token: String
    get() = lazy { prefer.getString("token", "") }.value.toString()
    set(value) {
        prefer.edit().putString("token", value).apply()
    }

val Context.sdf: SimpleDateFormat
    get() = lazy { SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA) }.value

fun Context.longToast(message: String) {
    toast(message, Toast.LENGTH_LONG)
}

fun Context.shortToast(message: String) {
    Toaster.show(message)
}

fun Context.toast(message: String, duration: Int) {
    val toast = Toast.makeText(this, message, duration)
    toast.setGravity(Gravity.CENTER, 0, 0)
    toast.show()
}


/**
 * 传实体类（继承Parcelable）
 */
fun Context.putModel(key: String, model: Parcelable, activity: Class<*>): Intent {
    val bundle = Bundle()
    bundle.putParcelable(key, model)
    val intent = Intent()
    intent.putExtras(bundle)
    intent.setClass(this, activity)
    return intent
}

/**
 * 获取实体类（继承Parcelable）
 */
fun <T : Parcelable> AppCompatActivity.getModel(key: String): T {
    val bundle = this.intent.extras
    return bundle?.getParcelable(key)!!
}

/**
 * 跳转activity
 */
fun Context.jump(activity: Class<*>, intent: Intent = Intent()) {
    intent.setClass(this, activity)
    startActivity(intent)
}


fun Any.toJson(): String {
    return Gson().toJson(this)
}


fun <E> List<E>.toJson(): String {
    return Gson().toJson(this)
}


fun String.getPhone(): String {
    try {
        var phone = this.substring(0, 3) + "****" + this.substring(this.length - 4, this.length)
        return phone
    } catch (e: Exception) {
        return this
    }
}

//fun <T> Context.refreshLoad(
//    page: Int,
//    refresh: SwipeRefreshLayout,
//    imgNoData: View,
//    it: List<T>,
//    allList: ArrayList<T>,
//    adapter: BaseAdapter<T>,
//    isZero: Boolean = false,
//    loadListener: RefreshLoadListener? = null
//) {
//    var startPage = 1
//    if (isZero) {
//        startPage = 0
//    } else {
//        startPage = 1
//    }
//    if (page == startPage) {
//        loadListener?.isLoadMore = true
//        allList.clear()
//        refresh.isRefreshing = false
//    }
//    if (it.isEmpty()) {
//        if (page > startPage) {
//            shortToast("没有更多数据")
//            loadListener?.isLoadMore = false
//            return
//        } else {
//            imgNoData.visibility = View.VISIBLE
//        }
//    } else {
//        allList.addAll(it)
//        imgNoData.visibility = View.GONE
//    }
//    adapter.datas = allList
//}

fun TextView.setSpannableString(
    start: Int = 0,
    startString: String = "",
    endString: String = "",
    end: Int = text.length,
    @ColorRes
    colorId: Int = 0,
    otherTextSize: Int = 0
) {
    val spannable = SpannableString(text)
    val startIndex: Int
    if (startString.isEmpty()) {
        startIndex = start
    } else {
        startIndex = text.indexOf(startString)
    }
    var endindex = end
    if (!endString.isEmpty()) {
        endindex = text.indexOf(endString)
    }
    if (colorId != 0) {
        val foregroundColorSpan = ForegroundColorSpan(ContextCompat.getColor(context, colorId))
        spannable.setSpan(
            foregroundColorSpan,
            startIndex,
            endindex,
            Spanned.SPAN_INCLUSIVE_INCLUSIVE
        )
    }
    if (otherTextSize != 0) {
        val absoluteSizeSpan = AbsoluteSizeSpan(otherTextSize, true)
        spannable.setSpan(absoluteSizeSpan, startIndex, endindex, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
    }
    text = spannable
}

fun String?.orEmpty(): String {
    return if (this == null || this.equals("null", ignoreCase = true)) "--" else this
}

